import { Role } from "../../Role";
import {
  RoleLowerBodyAnimatorEnum,
  RoleSkillType,
  RoleUpperBodyAnimatorEnum,
} from "../../Role.type";
import { RoleViewLowerBodyAnimator } from "../RoleViewLowerBodyAnimator";
import { RoleViewUpperBodyAnimator } from "../RoleViewUpperBodyAnimator";

export enum AnimatorSwitchType {
  /** 切换成功 */
  SwitchSuccess,
  /** 切换失败 */
  SwitchFail,
  /** 无需切换 */
  NoNeedSwitch,
}

/** 动画切换管理 */
export class AnimatorSwitchManage {
  role: Role;
  /** 角色上半身动画规则管理 */
  upperBodyAnimator: RoleViewUpperBodyAnimator = null!;
  /** 角色下半身动画规则管理 */
  lowerBodyAnimator: RoleViewLowerBodyAnimator = null!;

  /** 上半身当前状态 */
  get upperCurStateName() {
    return this.upperBodyAnimator.curStateName;
  }
  /** 下半身当前状态 */
  get lowerCurStateName() {
    return this.lowerBodyAnimator.curStateName;
  }

  /** 下半身待机 */
  lowerIdle(): AnimatorSwitchType {
    if (this.lowerCurStateName === RoleLowerBodyAnimatorEnum.Idle)
      return AnimatorSwitchType.NoNeedSwitch;
    this.lowerBodyAnimator.setTrigger(RoleLowerBodyAnimatorEnum.Idle);
    if (this.lowerBodyAnimator.checkTrans(RoleLowerBodyAnimatorEnum.Idle)) {
      return AnimatorSwitchType.SwitchSuccess;
    } else {
      return AnimatorSwitchType.SwitchFail;
    }
  }

  /** 下半身行走 */
  lowerWalk(): AnimatorSwitchType {
    if (this.lowerCurStateName === RoleLowerBodyAnimatorEnum.Walk)
      return AnimatorSwitchType.NoNeedSwitch;
    this.lowerBodyAnimator.setTrigger(RoleLowerBodyAnimatorEnum.Walk);
    if (this.lowerBodyAnimator.checkTrans(RoleLowerBodyAnimatorEnum.Walk)) {
      return AnimatorSwitchType.SwitchSuccess;
    } else {
      return AnimatorSwitchType.SwitchFail;
    }
  }

  /** 下半身跑步 */
  lowerRun(): AnimatorSwitchType {
    if (this.lowerCurStateName === RoleLowerBodyAnimatorEnum.Run)
      return AnimatorSwitchType.NoNeedSwitch;
    this.lowerBodyAnimator.setTrigger(RoleLowerBodyAnimatorEnum.Run);
    if (this.lowerBodyAnimator.checkTrans(RoleLowerBodyAnimatorEnum.Run)) {
      return AnimatorSwitchType.SwitchSuccess;
    } else {
      return AnimatorSwitchType.SwitchFail;
    }
  }

  /** 上半身待机 */
  upperIdle(): AnimatorSwitchType {
    if (this.upperCurStateName === RoleUpperBodyAnimatorEnum.Idle)
      return AnimatorSwitchType.NoNeedSwitch;
    this.upperBodyAnimator.setTrigger(RoleUpperBodyAnimatorEnum.Idle);
    if (this.upperBodyAnimator.checkTrans(RoleUpperBodyAnimatorEnum.Idle)) {
      return AnimatorSwitchType.SwitchSuccess;
    } else {
      return AnimatorSwitchType.SwitchFail;
    }
  }

  /** 上半身行走 */
  upperWalk(): AnimatorSwitchType {
    if (this.upperCurStateName === RoleUpperBodyAnimatorEnum.Walk)
      return AnimatorSwitchType.NoNeedSwitch;
    this.upperBodyAnimator.setTrigger(RoleUpperBodyAnimatorEnum.Walk);
    if (this.upperBodyAnimator.checkTrans(RoleUpperBodyAnimatorEnum.Walk)) {
      return AnimatorSwitchType.SwitchSuccess;
    } else {
      return AnimatorSwitchType.SwitchFail;
    }
  }

  /** 上半身跑步 */
  upperRun(): AnimatorSwitchType {
    if (this.upperCurStateName === RoleUpperBodyAnimatorEnum.Run)
      return AnimatorSwitchType.NoNeedSwitch;
    this.upperBodyAnimator.setTrigger(RoleUpperBodyAnimatorEnum.Run);
    if (this.upperBodyAnimator.checkTrans(RoleUpperBodyAnimatorEnum.Run)) {
      return AnimatorSwitchType.SwitchSuccess;
    } else {
      return AnimatorSwitchType.SwitchFail;
    }
  }

  /** 上半身释放技能 */
  upperSkill(type: RoleSkillType): AnimatorSwitchType {
    if (this.upperCurStateName === RoleUpperBodyAnimatorEnum.Skill)
      return AnimatorSwitchType.NoNeedSwitch;
    this.upperBodyAnimator.setTrigger(RoleUpperBodyAnimatorEnum.Skill);
    this.upperBodyAnimator.setNumber("skillType", type);
    if (this.upperBodyAnimator.checkTrans(RoleUpperBodyAnimatorEnum.Skill)) {
      return AnimatorSwitchType.SwitchSuccess;
    } else {
      return AnimatorSwitchType.SwitchFail;
    }
  }

  /** 成功匹配器 */
  matcherSuccess(a: AnimatorSwitchType, b: AnimatorSwitchType) {
    if (
      a === AnimatorSwitchType.SwitchSuccess &&
      b === AnimatorSwitchType.SwitchSuccess
    )
      return true;
    return false;
  }

  /** 模糊匹配器 */
  matcherVague(a: AnimatorSwitchType, b: AnimatorSwitchType) {
    if (
      a !== AnimatorSwitchType.SwitchFail &&
      b !== AnimatorSwitchType.SwitchFail
    )
      return true;
    return false;
  }

  constructor(
    upperBodyAnimator: RoleViewUpperBodyAnimator,
    lowerBodyAnimator: RoleViewLowerBodyAnimator
  ) {
    this.upperBodyAnimator = upperBodyAnimator;
    this.lowerBodyAnimator = lowerBodyAnimator;
  }
}
